﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;
using QDT.Common;
using QDT.Core.Members;
using QDT.Core.MSData;
using QDT.Core.Projects;
using QDT.MVC.Security;
using QDT.Services.Credits;
using QDT.Services.Deals;
using QDT.Services.Members;
using QDT.Services.Projects;
using QDT.Web.Code;

namespace QDT.Web.Moblie.Controllers
{
    public class ProjectController : UserBaseController
    {
        //
        // GET: /Project/

       private readonly IDealService _dealService;
       private readonly IProjectService _projectService;
       private readonly ICreditService _creditService;
        private const int ProjectFlag = 870;

        public ProjectController(IDealService dealService, IUserService userService, ICreditService creditService, IProjectService projectService)
        {
            _dealService = dealService;
            _projectService = projectService;
            _creditService = creditService;
        }

        public ActionResult Index()
        {
            return View();
        }

        [OutputCache(Duration = 30)]
        public ActionResult RepayList(int projectid, bool isAgree = false)
        {
            var list = IoC.Resolve<IDealService>().GetRepaymentScheme(projectid);
            if (isAgree)
            {
                return PartialView("_agreeRepayList", list);
            }
           
            return PartialView("NewRepayList", list);
        }

        public ActionResult NewProject(int id)
        {
            var project = _projectService.GetProject(id);
            if (project == null) return NotFound();
            if (!project.IsEffect || project.IsDelete) return NotFound();
            if (QDTContext.Current.IsAuthenticated && QDTContext.Current.User.GroupID != 1 && project.TypeID == 7 && project.ID > ProjectFlag)
            {
                return NotFound();
            }

            return View(project);
        }

        [HttpPost]
        [DefaultAuthorize]
        public ActionResult Bid(int id, decimal money, string safePassword)
        {
            if (money % 1 != 0)
            {
                return Error("投资金额需要填入正整数！");
            }

            var user = QDTContext.Current.User;

            if (user.GroupID == 1 && safePassword != "qianzhihe")
            {
                return Error("安全密码错误");
            }

            try
            {
                _dealService.Bid(user.ID, id, money);
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }

            return RedirectToAction("BidSuccess", new { projectId = id, loadMoney = money });
        }

        [DefaultAuthorize]
        public ActionResult BidSuccess(int projectId, decimal loadMoney)
        {

            if (!QDTContext.Current.IsAuthenticated) return HttpNotFound();
            var user = QDTContext.Current.User;


            var isOperat = DbProvider.D<UserPrize>().Any(x => x.UserId == user.ID);
           
            ViewBag.ProjectId = projectId.ToString(CultureInfo.InvariantCulture);
            ViewBag.LoadMoney = loadMoney.ToString(CultureInfo.InvariantCulture);
            ViewBag.OrderNum = new Random().Next(1000, 9999).ToString(CultureInfo.InvariantCulture);

            return Success("投资成功");
        }


        [OutputCache(Duration = 30)]
        public ActionResult AllBidList(int projectid)
        {
            var credits = _creditService.GetCreditList(null, projectid, null);
            return PartialView(credits);
        }

    }
}
